//
//  UTLHelpper.m
//  测试网络2
//
//  Created by air on 15/7/17.
//  Copyright (c) 2015年 chl. All rights reserved.
//

#import "UTLHelpper.h"
#import<SystemConfiguration/SystemConfiguration.h>
#import<netdb.h>
@implementation UTLHelpper
+ (BOOL)NetWorkIsOK//检查网络是否可用
{
//    if(
//       ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus]
//        != NotReachable)
//       &&
//       ([[Reachability reachabilityWithHostName:@"www.baidu.com"] currentReachabilityStatus]
//        != NotReachable)
//       ){
//        return YES;
//    }else{
//        return NO;
//    }
    
    return  [self connectedToNetwork];
}

+ (BOOL)connectedToNetwork{
   
    //创建零地址，0.0.0.0的地址表示查询本机的网络连接状态
   
    struct sockaddr_storage zeroAddress;
   
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.ss_len = sizeof(zeroAddress);
    zeroAddress.ss_family = AF_INET;
   
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
   
    //获得连接的标志
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
   
    //如果不能获取连接标志，则不能连接网络，直接返回
    if (!didRetrieveFlags)
    {
        return NO;
    }
    //根据获得的连接标志进行判断
   
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    return (isReachable&&!needsConnection) ? YES : NO;
}
 
@end
